<?php

/**
 * 获取栏目链接
 */
function uuu_url($cid = "", $key_array = null)
{
    if (empty($cid)) {
        return qqq_homeurl();
    }
    return uuu_getchannelurl($cid, $key_array);
}

/**
 * 获取系统变量
 * */
function uuu_cfg($strname, $return = 1)
{
    return uuu_str($strname, 0, $return);
}


/**
 * 获取所有的系统变量
 * @param int $cid
 * @return array
 */
function uuu_all_cfg($cid = 0)
{
    $ret = [];
    $str_table = tableex('str');
    $rows = M()->all_assoc("select strname,strvalue from {$str_table} where strcid=?", array($cid));
    foreach ($rows as $row) {
        $ret[$row['strname']] = $row['strvalue'];
    }

    return $ret;
}

/**
 * 获取栏目变量
 * */
function uuu_lm_str($strname, $cid = '', $return = 1)
{
    return uuu_str($strname, $cid, $return);
}


/**
 * 获取一篇文章
 * @param $where array 传入的查询条件
 * @return bool
 */
function uuu_art($where)
{
    $where['pagesize'] = 1;
    $article = uuu_alist($where);
    if (isset($article['list'][0])) {
        return $article['list'][0];
    } else {
        return false;
    }
}


/**
 * 列出子栏目,可以print_r($channel)查看栏目数组中的信息
 * @param int $kind 栏目的类型,1是单页栏目,2是文章栏目,3是链接
 * @param int $num 获取栏目的数量
 * @param int $fid 父栏目的cid,不填或填0则获取顶级栏目
 * @param int $shownav 填1则只返回后台勾选导航栏显示的栏目
 * @return array
 */
function uuu_channel_list($kind = 0, $num = 9999, $fid = 0, $shownav = 0)
{
    $channel = uuu_getchannelscache();
    $newchannel = array();
    $count = 0;
    if (isset($GLOBALS['nav'][$fid])) {
        if (is_array($GLOBALS['nav'][$fid])) {
            $diychannel = $GLOBALS['nav'][$fid];
        } elseif (function_exists($GLOBALS['nav'][$fid])) {
            $diychannel = $GLOBALS['nav'][$fid]($fid);
        }
        if (isset($diychannel)) {
            foreach ($diychannel as $val) {
                if ($shownav == 0 || !isset($val['ifshownav']) || ($shownav > 0 && @$val['ifshownav'])) {
                    $newchannel[] = $val;
                    $count++;
                }
                if ($count == $num) {
                    return $newchannel;
                }
            }
            return $newchannel;
        }
    }
    foreach ($channel as $val) {
        if ($fid == $val['fid']) {
            if ($shownav == 0 || ($shownav > 0 && $val['ifshownav'])) {
                if ($kind == 0) {
                    $newchannel[] = $val;
                    $count++;
                    $newchannel[count($newchannel) - 1]['key'] = $count;
                } elseif ($val['ckind'] == $kind) {
                    $newchannel[] = $val;
                    $count++;
                    $newchannel[count($newchannel) - 1]['key'] = $count;
                }
            }
        }
        if ($count == $num) {
            break;
        }
    }
    return $newchannel;
}

/**
 * 获取栏目的信息
 * @param string $kind name,cid,url,domain （name:栏目名，cid:当前栏目cid,url:链接,topfid:父级cid,domain:栏目域名）
 * @param int $cid 默认获取当前栏目的信息
 * @return array|bool|false|mixed|string
 */
function uuu_channel_info($kind = '', $cid = 0)
{
    if ($cid === 0) {
        if (!defined('cid')) {
            return false;
        }
        $cid = cid;
    }
    $channel = uuu_getchannelcache($cid);
    if (!$channel) {
        return false;
    }
    if (empty($kind)) {
        return $channel;
    } elseif ($kind == 'name' && isset($channel['cname'])) {
        return $channel['cname'];
    } elseif ($kind == 'topfid') {
        $breadcrumb = uuu_breadcrubm($channel['cid'], 1);
        if (isset($breadcrumb[0]['cid'])) {
            return $breadcrumb[0]['cid'];
        }
    } elseif ($kind == 'url') {
        return uuu_url($channel['cid']);
    } elseif ($kind == 'domain') {
        return @$channel['csetting']['channel_domain'];
    } else {
        if (isset($channel[$kind])) {
            return $channel[$kind];
        }
    }
    return false;
}

/**
 * 获取文章的链接
 * @param $value
 * @param $rowurl
 * @param $rowurltime
 * @param string $domain
 * @return mixed|string
 */
function uuu_aurl($value, $rowurl, $rowurltime, $domain = '')
{
    $thissetting = uuu_getchannelcache($value['cid']);
    if (empty($domain)) {
        if (isset($thissetting['csetting']['channel_domain']) && !empty($thissetting['csetting']['channel_domain'])) {
            $domains = explode(';', $thissetting['csetting']['channel_domain']);
            $domain = $domains[0];
            foreach ($domains as $key) {
                if ($key == server_name()) {
                    $domain = null;
                    break;
                }
            }
        }

    }
    if (empty($rowurl)) {
        if (empty($thissetting['csetting']['channel_articleurl'])) {
            return '';
        }
        $rowurl = $thissetting['csetting']['channel_articleurl'];
    }
    if (!empty($where['domain'])) {
        $domains = explode(';', $where['domain']);
        $where['domain'] = $domains[0];
        foreach ($domains as $domain) {
            if ($domain == server_name()) {
                $where['domain'] = null;
                break;
            }
        }
    }
    unset($thissetting);
    $rowurls = explode(';', $rowurl);
    //增加多个路由匹配失败时尝试后面的
    for ($iii = 0; $iii < count($rowurls); $iii++) {
        $rowurl = $rowurls[$iii];
        //这里需要排除/,否则会全部贯穿
        preg_match_all("/{(.*)}|\[(.*)\]|\((.*)\)/isU", $rowurl, $match);
        if (count($match[0]) > 0) {
            foreach ($match[0] as $key => $val) {
                if ($rowurltime > 0 && ($val == '(y)' || $val == '(m)' || $val == '(d)')) {
                    !isset($value['y']) and $value['y'] = date('Y', $rowurltime);
                    !isset($value['m']) and $value['m'] = date('m', $rowurltime);
                    !isset($value['d']) and $value['d'] = date('d', $rowurltime);
                }

                if (isset($value[$match[1][$key]]) && $value[$match[1][$key]] != "" && $value[$match[1][$key]] != null) {
                    if (is_numeric($value[$match[1][$key]])) {
                        $rowurl = str_replace($val, $value[$match[1][$key]], $rowurl);
                    } else {
                        $rowurl = str_replace($val, urlencode($value[$match[1][$key]]), $rowurl);
                    }
                }
                if (isset($value[$match[2][$key]]) && $value[$match[2][$key]] != "" && $value[$match[2][$key]] != null) {
                    if (is_numeric($value[$match[2][$key]])) {
                        $rowurl = str_replace($val, $value[$match[2][$key]], $rowurl);
                    } else {
                        $rowurl = str_replace($val, urlencode($value[$match[2][$key]]), $rowurl);
                    }
                }
                if (isset($value[$match[3][$key]]) && $value[$match[3][$key]] != "" && $value[$match[3][$key]] != null) {
                    if (is_numeric($value[$match[3][$key]])) {
                        $rowurl = str_replace($val, $value[$match[3][$key]], $rowurl);
                    } else {
                        $rowurl = str_replace($val, urlencode($value[$match[3][$key]]), $rowurl);
                    }
                }
            }

            if ($rowurl != $rowurls[$iii]) {
                break;
            }
        }

    }
    if (!empty($domain)) {
        $domain = '//' . $domain . server_port();
    }
    if (UrlRewrite) {
        $rowurl = $domain . SystemDir . $rowurl;
        $rowurl = str_replace("//", "/", $rowurl);
    } else {
        $rowurl = $domain . SystemDir . IndexFile . $rowurl;
    }
    return $rowurl;
}

/** 返回文章的列表
 * @param array $where 查询条件
 * @return array
 */
function uuu_alist($where = array())
{
    if (isset($where['page']) && !is_numeric($where['page'])) {
        if (isset($_GET[$where['page']]) && $_GET[$where['page']] > 0) {
            $where['page'] = intval($_GET[$where['page']]);
        } else {
            $where['page'] = 1;
        }
    }
    $nullarticle = array('list' => array(), 'pagecount' => 0, 'pagesize' => 1, 'page' => 1,);
    if (!isset($where['cid']) && !defined('cid') && !isset($where['tablename']) && !isset($where['cids'])) {
        return $nullarticle;
    }

    if (!isset($where['tablename']) && !isset($where['cid']) && !isset($where['cids']) && defined('cid')) {
        $where['cid'] = cid;
    }

    if (!isset($where['cid'])) {
        $where['cid'] = 0;
    }
    if (is_numeric($where['cid'])) {
        $where['cid'] = intval($where['cid']);
    }
    if (isset($where['cache']) && $where['cache'] > 0) {
        $cachehash = md5(json_encode($where));
        $res = cache_site_get($cachehash, $where['cache'], 'alist');
        if ($res) {
            return json_decode($res, 1);
        }
    }

    $sql = '';

    if ($where['cid'] !== 0) {
        $thischannelcache = uuu_getchannelcache($where['cid']);
        //echo "不是0";
        //var_dump($thischannelcache);
        if (!$thischannelcache) {
            return $nullarticle;
        }
        $where['cid'] = $thischannelcache['cid'];
        if ($thischannelcache['ifshow'] == 0) {
            $where['all'] = 0;
        }
    }

    if (!isset($where['tablename'])) {
        $where['tablename'] = '';
    }
    if (!isset($where['all']) || $where['all'] == 1) {
        if (!isset($where['cids'])) {
            $where['cids'] = '';
        }
        $clistsetting = uuu_alist_getson($where['cid'], $where['tablename'], $where['cids']);
        if (!isset($clistsetting['sql'])) {
            return $nullarticle;
        }
        $where['cidsql'] = $clistsetting['sql'];
        $where['tablename'] = $clistsetting['tablename'];
        $defaultsetting = uuu_getchannelcache($clistsetting['default']);
        if (!isset($where['pageurl']) && !isset($clistsetting['pageurl'])) {
            unset($defaultsetting['csetting']['channel_pageurl']);
        }
    } else {
        if (!isset($where['cidsql'])) {
            $where['cidsql'] = " and (cid='" . $where['cid'] . "')";
        }
        if ($where['cid']) {
            $defaultsetting = uuu_getchannelcache($where['cid']);
            if (empty($where['tablename']) && isset($defaultsetting['csetting']['articletable'])) {
                $where['tablename'] = tableex($defaultsetting['csetting']['articletable']);
            }
        }
    }
    $sql .= $where['cidsql'];
    //var_dump($where);
    if (!isset($where['pagesize'])) {
        if (isset($defaultsetting['csetting']['temppage_size']) && !empty($defaultsetting['csetting']['temppage_size'])) {
            $where['pagesize'] = $defaultsetting['csetting']['temppage_size'];
        }
    }
    if (!isset($where['order'])) {
        if (isset($defaultsetting['csetting']['temppage_order']) && !empty($defaultsetting['csetting']['temppage_order'])) {
            $where['order'] = $defaultsetting['csetting']['temppage_order'];
        }
    }
    isset($where['addslashes']) ? $ifaddslashes = $where['addslashes'] : $ifaddslashes = 1;
    !isset($where['tablename']) and die('no tablename');
    isset($where['pagesize']) ? $where['pagesize'] = intval($where['pagesize']) : $where['pagesize'] = 9;
    isset($where['order']) ? $where['order'] = 'order by ' . $where['order'] : $where['order'] = 'order by id desc';
    isset($where['start']) ? $where['start'] = intval($where['start']) : $where['start'] = 0;
    !isset($where['column']) and $where['column'] = '*';
    isset($where['rowidstyle']) && !isset($where['rowidname']) and $where['rowidname'] = 'id';
    !isset($where['linktime']) and $where['linktime'] = 'posttime';
    !isset($where['link']) and $where['link'] = '';
    !isset($where['domain']) and $where['domain'] = '';

    if (trim($where['column']) != '*') {
        $colums = explode(",", $where['column']);
        array_trim($colums);
        if (!in_array("id", $colums)) {
            $colums[] = "id";
        }
        if (!in_array("cid", $colums)) {
            $colums[] = "cid";
        }
        $where['column'] = join(',', $colums);
    }

    if (isset($where['where'])) {
        if (is_array($where['where'])) {
            foreach ($where['where'] as $key => $val) {
                $key = uuu_addslashes_str($key, $ifaddslashes);
                if (is_array($val) && count($val) > 0) {
                    $sql .= " and (";
                    foreach ($val as $value) {
                        $sql .= $key . "='" . uuu_addslashes_str($value, $ifaddslashes) . "' or ";
                    }
                    $sql = substr($sql, 0, strlen($sql) - 4);
                    $sql .= ")";
                } else {
                    $sql .= " and " . $key . "='" . uuu_addslashes_str($val, $ifaddslashes) . "'";
                }
            }
        } else {
            $sql .= " and " . $where['where'];
        }
    }
    if (isset($where['orwhere'])) {
        foreach ($where['orwhere'] as $key => $val) {
            if (!is_array($val)) {
                $val = array();
                $val[] = $where['orwhere'][$key];
            }
            $key = uuu_addslashes_str($key, $ifaddslashes);

            foreach ($val as $val1) {
                if (is_numeric($val1) && $val1 > 0) {
                    $sql .= " and (" . $key . "='[" . $val1 . "]' or " . $key . " like '%," . $val1 . ",%' or " . $key . " like '[" . $val1 . ",%' or " . $key . " like '%," . $val1 . "]')";
                } else {
                    unset($where['orwhere'][$key]);
                }
            }
        }
    }

    if (isset($where['inwhere'])) {
        foreach ($where['inwhere'] as $key => $val) {
            if (!is_array($val)) {
                $val = array();
                $val[] = $where['inwhere'][$key];
            }
            $key = uuu_addslashes_str($key, $ifaddslashes);
            $in = array();
            foreach ($val as $val1) {
                if (is_numeric($val1) && $val1 > 0) {
                    $in[] = $val1;
                } else {
                    unset($where['inwhere'][$key]);
                }
            }

            if (count($in)) {
                $sql .= " and {$key} in (" . join(",", $in) . ")";
            }
        }
    }
    if (isset($where['nowhere'])) {
        foreach ($where['nowhere'] as $key => $val) {
            $key = uuu_addslashes_str($key, $ifaddslashes);
            if (is_array($val) && count($val) > 0) {
                foreach ($val as $value) {
                    $sql .= " and " . $key . "<>'" . uuu_addslashes_str($value, $ifaddslashes) . '\'';
                }
            } else {
                $sql .= " and " . $key . "<>'" . uuu_addslashes_str($val, $ifaddslashes) . "'";
            }
        }
    }
    if (isset($where['other']) && !empty($where['other'])) {
        $sql .= " and " . $where['other'];
    }

    if (isset($where['keyword']) && !empty($where['keyword'])) {
        if (!isset($where['searchcolumn'])) {
            $where['searchcolumn'] = 'title';
        }
        if (!is_array($where['searchcolumn'])) {
            $where['searchcolumn'] = explode(',', $where['searchcolumn']);
        }
        if (!is_array($where['keyword'])) {
            $where['keyword'] = explode('|', $where['keyword']);
        }
        $sql .= " and (";
        foreach ($where['searchcolumn'] as $key => $searchcolumn) {
            foreach ($where['keyword'] as $keywordkey => $keywordval) {
                if ($key == 0 && $keywordkey == 0) {
                    $sql .= "$searchcolumn like '%" . uuu_addslashes_str($keywordval, $ifaddslashes) . "%'";
                } else {
                    $sql .= " or $searchcolumn like '%" . uuu_addslashes_str($keywordval, $ifaddslashes) . "%'";
                }
            }
        }
        $sql .= " )";
    }
    if (!empty($sql)) {
        $sql = 'where' . ltrim($sql, 'and ');
    }
    if (isset($where['page'])) {
        $where['page'] = intval($where['page']);
        //$where['page'] = intval($where['page']);
        $where['pagecount'] = M()->fetchcount("SELECT count(id) FROM " . tableex($where['tablename']) . " $sql");
        $where['articlecount'] = $where['pagecount'];
        $where['start'] = ($where['page'] - 1) * $where['pagesize'];
        if ($where['articlecount'] % $where['pagesize'] == 0) {
            $where['pages'] = ($where['articlecount'] / $where['pagesize']);
        } else {
            $where['pages'] = intval($where['articlecount'] / $where['pagesize']) + 1;
        }
        if (isset($defaultsetting['csetting']['channel_pageurl']) && !empty($defaultsetting['csetting']['channel_pageurl']) && !isset($where['pageurl'])) {
            $allpageurl = explode(';', $defaultsetting['csetting']['channel_pageurl']);
            if (UrlRewrite) {
//                $where['pageurl'] = $allpageurl[0];
                $thisurl = SystemDir . $allpageurl[0];
                $thisurl = str_replace("//", "/", $thisurl);
                $where['pageurl'] = $thisurl;
            } else {
                $where['pageurl'] = SystemDir . IndexFile . $allpageurl[0];
            }
        }
    }

    log_time_start("sql");
    if (!isset($where['sql'])) {
        $where['sql'] = 'SELECT ' . $where['column'] . ' FROM ' . tableex($where['tablename']) . ' ' . $sql . ' ' . $where['order'] . ' limit ' . $where['start'] . ',' . $where['pagesize'];
    }


    $articles = M()->all($where['sql']);
    $i = 0;
    foreach ($articles as $value) {
        $i++;
        $value['key'] = $i;
        if (!isset($value[$where['linktime']])) {
            $value[$where['linktime']] = 0;
        }
        $value['link'] = uuu_aurl($value, $where['link'], $value[$where['linktime']], $where['domain']);
        $where['list'][] = $value;
    }
    if (!isset($where['list'])) {
        $where['list'] = array();
    }
    if (isset($where['cache']) && $where['cache'] > 0) {
        $cachehash = md5(json_encode($where));
        cache_site_set($cachehash, json_encode($where), $where['cache'], 'alist');
    }

    //log_time_end("sql");
    //var_dump($where);
    return $where;
}

/**
 * 获取子分类及自身
 * @param string $cid 栏目id
 * @param string $fid 分类id
 * @return array
 */
function uuu_getkinds($cid, $fid)
{
    $fid = intval($fid);
    $kindsarray = array($fid);
    if (is_numeric($cid)) {
        $thichannelcache = uuu_getchannelcache($cid);
        $defaultsetting = $thichannelcache['csetting'];
        if (isset($defaultsetting['articletable']) && !empty($defaultsetting['articletable'])) {
            $kindtable = tableex($defaultsetting['articletable']);
        } else {
            return $kindsarray;
        }
    } else {
        $kindtable = tableex($cid);
    }
    M()->query("SELECT id FROM $kindtable where fid='$fid';");
    $link = M()->fetchall();
    foreach ($link as $value) {
        $thisallkinds = uuu_getkinds($kindtable, $value['id']);
        foreach ($thisallkinds as $thisvalue) {
            $kindsarray[] = $thisvalue;
        }
    }
    return $kindsarray;
}

/**
 * 文章列表，包含子栏目
 * @param $cid
 * @param $tablename
 * @param $cids
 * @param int $first
 * @return array
 */
function uuu_alist_getson($cid, $tablename, $cids, $first = 1)
{
    if ($cid == 0 && empty($tablename)) {
        $tablename = tableex(ArticleTable);
    }
    $array = array();
    $array['list'] = array();
    $channels = uuu_getchannelscache();
    if ($first) {
        if (!is_array($cids)) {
            $cids = str_replace(',', ';', $cids);
            $cids = explode(';', $cids);
        }
        $thischannel = uuu_getchannelcache($cid);
        if ($thischannel && @$thischannel['ckind'] == 2 && (in_array($cid, $cids) || empty($cids[0]))) {
            if (isset($thischannel['csetting']['articletable']) && !empty($thischannel['csetting']['articletable']) && empty($tablename)) {
                $tablename = $thischannel['csetting']['articletable'];
            }
            if ($tablename == $thischannel['csetting']['articletable']) {
                $array['list'][] = $thischannel;
                if (isset($thischannel['csetting']['channel_pageurl']) && !empty($thischannel['csetting']['channel_pageurl'])) {
                    $array['pageurl'] = $thischannel['csetting']['channel_pageurl'];
                }
            }
        }
        $array['tablename'] = $tablename;
    }
    foreach ($channels as $key => $val) {
        if ($val['fid'] == $cid) {
            $thischannel = uuu_getchannelcache($val['cid']);
            if (empty($tablename)) {
                if ($thischannel['ckind'] == 2 && isset($thischannel['csetting']['articletable']) && (in_array($val['cid'], $cids) || empty($cids[0]))) {
                    $array['list'][] = $val;
                    $tablename = $thischannel['csetting']['articletable'];
                    $array['tablename'] = $tablename;
                }
            } else {
                if ($thischannel['ckind'] == 2 && isset($thischannel['csetting']['articletable']) && $thischannel['csetting']['articletable'] == $tablename && (in_array($val['cid'], $cids) || empty($cids[0]))) {
                    $array['list'][] = $val;
                }
            }

            $sonarray = uuu_alist_getson($val['cid'], $tablename, $cids, 0);
            foreach ($sonarray['list'] as $key1 => $val1) {
                $array['list'][] = $val1;
            }
        }
    }
    if ($first) {
        $sql = '';
        foreach ($array['list'] as $key => $val) {
            if ($key == 0) {
                $sql .= " and (cid='" . $val['cid'] . "'";
            } elseif ($key == (count($array['list']) - 1)) {
                $sql .= " or cid='" . $val['cid'] . "'";
            } else {
                $sql .= " or cid='" . $val['cid'] . "'";
            }
        }
        if (count($array['list']) > 0) {
            $array['sql'] = $sql . ')';
        }
        if (isset($array['list'][0])) {
            $array['default'] = $array['list'][0]['cid'];
        } else {
            $array['default'] = 0;
        }
        return $array;
    } else {
        return $array;
    }
}

/**
 * 插入文章
 * @param array $article
 * @param string $setting
 * @return bool|int|string
 */
function uuu_ainsert($article, $setting = '')
{
    if (!is_array($article)) {
        return false;
    }
    if (empty($setting)) {
        $setting = array();
    }
    if (!isset($setting['cid']) && !isset($article['cid'])) {
        if (defined('cid')) {
            $setting['cid'] = cid;
        } else {
            return 'setting no cid';
        }
    }
    if (!isset($setting['addslashes'])) {
        $setting['addslashes'] = 1;
    }
    if (!isset($setting['default'])) {
        $setting['default'] = 1;
    }
    if (!isset($setting['check'])) {
        $setting['check'] = 1;
    }
    if (!isset($setting['cid'])) {
        $setting['cid'] = $article['cid'];
    }
    if (isset($article['cid'])) {
        unset($article['cid']);
    }

    $channel = uuu_getchannelcache($setting['cid']);
    if (!$channel) {
        return 'channel not exist';
    }
    $setting['cid'] = $channel['cid'];
    $csetting = $channel['csetting'];
    if (!isset($setting['tablename']) || empty($setting['tablename'])) {
        $setting['tablename'] = $csetting['articletable'];
    }
    if (count($article) == 0) {
        return 'where article columns';
    }
    foreach ($article as $key => $val) {
        $article[$key] = uuu_addslashes_str($article[$key], $setting['addslashes']);
    }
    if ($setting['check']) {
        $columns = M()->all("select mname,mkind,ifonly,strdefault,msetting from " . tableex('moudle') . " where cid=" . $setting['cid'] . "  order by morder asc;");
        foreach ($columns as $thiscolumn) {
            $key = $thiscolumn['mname'];
            if (!isset($article[$key]) && $setting['default']) {
                $article[$key] = $thiscolumn['strdefault'];
                if ($thiscolumn['mkind'] == 9) {
                    if ($thiscolumn['strdefault'] == 'now') {
                        $article[$key] = time();
                    } else {
                        $thiscolumn['strdefault'] = @strtotime($thiscolumn['strdefault']);
                        if ($thiscolumn['strdefault']) {
                            $article[$key] = $thiscolumn['strdefault'];
                        } else {
                            $article[$key] = 0;
                        }
                    }
                }
            }
            if (isset($article[$key])) {
                $thismsetting = json_decode($thiscolumn['msetting'], 1);
                $thismsetting['ifonly'] = $thiscolumn['ifonly'];
                $thismsetting['tablename'] = $setting['tablename'];
                $thismoudlevalue = $article[$key];
                if ($thismsetting['filterhtml'] == 1) {
                    $thismoudlevalue = kses($thismoudlevalue);
                } elseif ($thismsetting['filterhtml'] == 2) {
                    $thismoudlevalue = htmlspecialchars($thismoudlevalue);
                }
                $acheck = uuu_acheck($key, $thismoudlevalue, $setting['cid'], $thismsetting);
                if ($acheck === 'error') {
                    return $key . ' error';
                }
                if ($acheck === 'short') {
                    return $key . ' short';
                }
                if ($acheck === 'long') {
                    return $key . ' long';
                }
                if ($acheck === 'exist') {
                    return $key . ' exist';
                }
                $article[$key] = $thismoudlevalue;
            }
        }
    }
    $article['cid'] = $setting['cid'];
    if (!isset($article['posttime'])) {
        $article['posttime'] = time();
    }
    if (!isset($article['adminuid'])) {
        $article['adminuid'] = 0;
    }
    $result = M()->insert($setting['tablename'], $article);
    if ($result) {
        return $result;
    } else {
        return false;
    }
}

/**
 * 编辑文章
 * @param $article
 * @param string $setting
 * @return bool|string
 */
function uuu_aedit($article, $setting = '')
{
    if (!isset($article['id']) || !is_numeric($article['id']) || $article['id'] < 1) {
        return 'id error';
    }
    $id = $article['id'];
    unset($article['id']);
    if (!is_array($article)) {
        return false;
    }
    if (empty($setting)) {
        $setting = array();
    }
    if (!isset($setting['cid']) && !isset($article['cid'])) {
        if (defined('cid')) {
            $setting['cid'] = cid;
        } else {
            return 'setting no cid';
        }
    }
    if (!isset($setting['addslashes'])) {
        $setting['addslashes'] = 1;
    }
    if (!isset($setting['check'])) {
        $setting['check'] = 1;
    }
    if (!isset($setting['cid'])) {
        $setting['cid'] = $article['cid'];
    }
    if (isset($article['cid'])) {
        unset($article['cid']);
    }
    $channel = uuu_getchannelcache($setting['cid']);
    if (!$channel) {
        return 'channel not exist';
    }
    $setting['cid'] = $channel['cid'];
    $csetting = $channel['csetting'];
    if (!isset($setting['tablename']) || empty($setting['tablename'])) {
        $setting['tablename'] = $csetting['articletable'];
    }
    if (count($article) == 0) {
        return 'where article columns';
    }
    foreach ($article as $key => $val) {
        $article[$key] = uuu_addslashes_str($article[$key], $setting['addslashes']);
    }
    if ($setting['check']) {
        $columns = M()->all("select mname,ifonly,strdefault,msetting from " . tableex('moudle') . " where cid=" . $setting['cid'] . "  order by morder asc;");
        foreach ($columns as $thiscolumn) {
            $key = $thiscolumn['mname'];
            if (isset($article[$key])) {
                $thismsetting = json_decode($thiscolumn['msetting'], 1);
                $thismsetting['ifonly'] = $thiscolumn['ifonly'];
                $thismsetting['tablename'] = $setting['tablename'];
                $thismoudlevalue = $article[$key];
                if ($thismsetting['filterhtml'] == 1) {
                    $thismoudlevalue = kses($thismoudlevalue);
                } elseif ($thismsetting['filterhtml'] == 2) {
                    $thismoudlevalue = htmlspecialchars($thismoudlevalue);
                }
                $acheck = uuu_acheck($key, $thismoudlevalue, $setting['cid'], $thismsetting, $id);
                if ($acheck === 'error') {
                    return $key . ' error';
                }
                if ($acheck === 'short') {
                    return $key . ' short';
                }
                if ($acheck === 'long') {
                    return $key . ' long';
                }
                if ($acheck === 'exist') {
                    return $key . ' exist';
                }
                $article[$key] = $thismoudlevalue;
            }
        }
    }
    //一路有你，可加入原始语句
    if (isset($setting['sql'])) {
        $article['sql'] = $setting['sql'];
    }
    $result = M()->update($setting['tablename'], "id='$id'", $article);
    if ($result) {
        return true;
    } else {
        return false;
    }
}

/**
 * 根据文章id删除文章
 * @param $cid
 * @param $id
 * @param string $tablename
 * @return bool|string
 */
function uuu_adel($cid, $id, $tablename = '')
{
    if (empty($cid) && empty($tablename)) {
        if (defined('cid')) {
            $cid = cid;
        } else {
            return 'no cid';
        }
    }
    if (empty($id)) {
        return 'no id';
    } else {
        $id = intval($id);
    }
    if (empty($tablename)) {
        $channel = uuu_getchannelcache($cid);
        if (!$channel) {
            return 'channel not exist';
        }
        $csetting = $channel['csetting'];
        $tablename = $csetting['articletable'];
    }
    $query = M()->query("delete from `$tablename` WHERE id='$id'");
    if ($query) {
        return true;
    } else {
        return false;
    }
}

/**
 * 对文章进行检测，是否存在等
 * @param $mname
 * @param $value
 * @param string $cid
 * @param string $thismsetting
 * @param int $id
 * @return bool|string
 */
function uuu_acheck($mname, $value, $cid = '', $thismsetting = '', $id = 0)
{
    if ($cid == '') {
        if (defined('cid')) {
            $cid = cid;
        } else {
            return 'no cid';
        }
    }
    if ($thismsetting == '') {
        $column = M()->one("select mname,ifonly,msetting from " . tableex('moudle') . " where cid='" . $cid . "' and mname='" . $mname . "' limit 1;");
        $thismsetting = json_decode($column['msetting'], 1);
        $thismsetting['ifonly'] = $column['ifonly'];
    }
    if (isset($thismsetting['regular']) && strlen($thismsetting['regular']) > 3 && !empty($value)) {
        if (!preg_match($thismsetting['regular'], $value)) {
            return 'error';
        }
    }
    if (isset($thismsetting['lenmin']) && is_numeric($thismsetting['lenmin']) && $thismsetting['lenmin'] > 0) {
        if (strlen($value) < $thismsetting['lenmin']) {
            return 'short';
        }
    }
    if (isset($thismsetting['lenmax']) && is_numeric($thismsetting['lenmax']) && $thismsetting['lenmax'] > 0) {
        if (strlen($value) > $thismsetting['lenmax']) {
            return 'long';
        }
    }
    if ($thismsetting['ifonly'] == 1) {
        if (!isset($thismsetting['tablename'])) {
            $channel = uuu_getchannelcache($cid);
            $thismsetting['tablename'] = $channel['csetting']['articletable'];
        }
        if ($id > 0) {
            $pagecount = M()->fetchcount("SELECT id FROM " . tableex($thismsetting['tablename']) . " where $mname='$value' and cid='" . $cid . "' and id<>'$id' limit 1;");
        } else {
            $pagecount = M()->fetchcount("SELECT id FROM " . tableex($thismsetting['tablename']) . " where $mname='$value' and cid='" . $cid . "' limit 1;");
        }
        if ($pagecount) {
            return 'exist';
        }
    }
    return true;
}

/**
 * 对字符串的双引号进行转义
 * @param $val
 * @param $addslashes
 * @return mixed
 */
function uuu_addslashes_str($val, $addslashes)
{
    if ($addslashes) {
        return dbstr($val);
    } else {
        return $val;
    }
}

/**
 * 分页
 * @param array $alist 文章列表
 * @param string $url 分页路径
 * @param int $returnarray 是否返回数组
 * @return array|bool|string
 */
function uuu_pagelist($alist, $url = '', $returnarray = 0)
{
//    var_dump($alist);
    if (!isset($alist['pagecount']) || !isset($alist['page'])) {
        return false;
    }
    $recordcount = $alist['pagecount'];
    $pagesize = $alist['pagesize'];
    $curpage = $alist['page'];
    $pages = isset($alist['showpages']) ? $alist['showpages'] : 3;
    if (empty($url) && isset($alist['pageurl']) && !empty($alist['pageurl'])) {
        $url = $alist['pageurl'];
        if (isset($alist['cid'])) {
            $url = str_replace('(cid)', $alist['cid'], $url);
            $url = SystemDir . $url;
            $url = str_replace("//", "/", $url);
        }
    } else {
        if (UrlRewrite) {
            $url = $_SERVER['REQUEST_URI'];
            $url = SystemDir . $url;
            $url = str_replace("//", "/", $url);
        } else {
            $url = $_SERVER['SCRIPT_NAME'] . SystemDir . $_SERVER['REQUEST_URI'];
            $url = str_replace("//", "/", $url);
        }
        if (stripos($url, 'page=') === false) {
            if (stripos($url, '?') === false) {
                $url = $url . "?page=(page)";
            } else {
                $url = $url . "&page=(page)";
            }
        }
        $url = preg_replace("/page=([0-9]+)/is", "page=(page)", $url);
    }
    if ($pagesize < 1) {
        $pagesize = 1;
    }
    $pagesarray = array();
    $totalpage = max(ceil($recordcount / $pagesize), 1);
    if ($curpage < 0 || $curpage > $totalpage) $curpage = 1;
    $pageno = $curpage;
    if ($pageno < 0 || $pageno > $totalpage) $pageno = 1;
    if ($pageno == 1) {
        $pagesarray[] = array('url' => str_replace('(page)', '1', $url), 'title' => '&lt;&lt;', 'class' => 'disabled');
    } else {
        $pagesarray[] = array('url' => str_replace('(page)', '1', $url), 'title' => '&lt;&lt;', 'class' => '');
    }

    if ($pages > $totalpage) {
        $startpage = 1;
        $endpage = min($startpage + $pages, $totalpage);
    } else {
        $startpage = max($totalpage - $pages, 1);
        $startpage = min($startpage, $curpage);
        $endpage = min($startpage + $pages, $totalpage);
    }
    if ($pageno > 1) {
        $pagesarray[] = array('url' => str_replace('(page)', $pageno - 1, $url), 'title' => '&lt;', 'class' => '');
    } else {
        $pagesarray[] = array('url' => str_replace('(page)', 1, $url), 'title' => '&lt;', 'class' => 'disabled');
    }
    for ($i = $startpage; $i <= $endpage; $i++) {
        if ($curpage == $i) {
            $pagesarray[] = array('url' => str_replace('(page)', $i, $url), 'title' => $i, 'class' => 'on');
        } else {
            $pagesarray[] = array('url' => str_replace('(page)', $i, $url), 'title' => $i, 'class' => '');
        }
    }
    if ($pageno < $totalpage) {
        $pagesarray[] = array('url' => str_replace('(page)', $pageno + 1, $url), 'title' => '&gt;', 'class' => '');
    } else {
        $pagesarray[] = array('url' => str_replace('(page)', $totalpage, $url), 'title' => '&gt;', 'class' => 'disabled');
    }
    if ($pageno == $totalpage) {
        $pagesarray[] = array('url' => str_replace('(page)', $totalpage, $url), 'title' => '&gt;&gt;', 'class' => 'disabled');
    } else {
        $pagesarray[] = array('url' => str_replace('(page)', $totalpage, $url), 'title' => '&gt;&gt;', 'class' => '');
    }
    if ($returnarray) {
        return $pagesarray;
    } else {
        $outhtml = '';
        foreach ($pagesarray as $val) {
            if (!empty($val['class'])) {
                $val['class'] = ' class="' . $val['class'] . '"';
            }
            $outhtml .= '<li ' . $val['class'] . ' ><a href="' . $val['url'] . '">' . $val['title'] . '</a></li>';
        }
    }
    return $outhtml;
}

/**
 * 获取导航条
 * @param int $where
 * @return array|string
 */
function uuu_nav($where = 0)
{
    $channelarray = array();
    if (!is_array($where)) {
        $thiscid = $where;
        $where = array();
        $where['cid'] = $thiscid;
    }
    if (!isset($where['cid'])) {
        $where['cid'] = 0;
    }
    $cid = $where['cid'];
    if (!isset($where['oncid'])) {
        if (defined('cid')) {
            $where['oncid'] = cid;
        } else {
            $where['oncid'] = 0;
        }
        //取出on的所有id
    }

    if (!isset($where['bro'])) {
        $where['bro'] = 1;
    }
    if (!isset($where['papa'])) {
        $where['papa'] = 1;
    }
    if ($where['cid'] == 0) {
        if (!isset($where['homepage'])) {
            $where['homepage']['name'] = '首页';
            $where['homepage']['url'] = qqq_homeurl();
            $where['homepage']['target'] = '';
        } else {
            if (is_array($where['homepage'])) {
                if (!isset($where['homepage']['name'])) {
                    $where['homepage']['name'] = '首页';
                }
                if (!isset($where['homepage']['url'])) {
                    $where['homepage']['url'] = qqq_homeurl();
                }
                if (!isset($where['homepage']['target'])) {
                    $where['homepage']['target'] = '';
                }
            }
        }
        if (is_array($where['homepage'])) {
            $channelarray[] = array('cid' => '0', 'fid' => '0', 'name' => $where['homepage']['name'], 'url' => $where['homepage']['url'], 'target' => $where['homepage']['target']);
        }
    }
    $allchannels = uuu_getchannelscache();
    $channels = array();
    foreach ($allchannels as $val) {
        if ($val['ifshownav'] == 1) {
            $channels[] = $val;
        }
    }
    $count = 0;
    $myfid = 0;
    foreach ($channels as $channel) {
        if (is_numeric($cid)) {
            if ($channel['cid'] == $cid) {
                $myfid = $channel['fid'];
                if ($cid > 0 && $where['papa'] == 1) {
                    $channelarray['papa'] = $myfid;//显示父栏目
                }
            }
            if ($channel['fid'] == $cid) {
                $count = 1;
                $channelarray[] = $channel;
            }
        } elseif (is_array($cid)) {
            if (in_array($channel['cid'], $cid)) {
                $count = 1;
                $channelarray[] = $channel;
            }
        }
    }
    if ($count > 0 && is_numeric($cid) && $cid > 0 && $where['papa'] == 1) {
        $channelarray['papa'] = $cid;
    }
    if ($count == 0) {
        $noonpapaid = $myfid;
    } else {
        $noonpapaid = false;
    }
    if (isset($channelarray['papa']) && $channelarray['papa'] > 0) {
        foreach ($channels as $channel) {
            if ($channel['cid'] == $channelarray['papa']) {
                $channelarray['papa'] = $channel;
            }
        }
    } else {
        unset($channelarray['papa']);
    }
    if (isset($where['bro']) && $where['bro'] == 1 && $count == 0 && is_numeric($cid)) {
        //显示兄弟栏目
        if (isset($myfid) && $myfid > 0) {
            foreach ($channels as $key => $channel) {
                if ($channel['fid'] == $myfid) {
                    $channelarray[] = $channel;
                }
            }
        } elseif (isset($myfid) && $myfid == 0) {
            foreach ($channels as $key => $channel) {
                if ($channel['cid'] == $cid) {
                    $channelarray[] = $channel;
                }
            }
        }
    }
    $onarray = array();
    if (defined('cid')) {
        $thiscid = cid;
        while (1 < 2) {
            $ifin = false;
            foreach ($channels as $channel) {
                if ($channel['cid'] == $thiscid && $channel['cid'] != $noonpapaid) {
                    $onarray[] = $channel['cid'];
                    $thiscid = $channel['fid'];
                    $ifin = true;
                    break;
                }
            }
            if ($ifin === false || $thiscid == 0) {
                break;
            }
        }
    }
    $returnarray = array();
    $returnstr = '';
    if (!isset($where['html'])) {
        $where['html'] = '<li {onstyle} ><a href="{url}" {target} >{name}</a></li>';
    }
    if (isset($GLOBALS['nav'][$cid])) {
        if (is_array($GLOBALS['nav'][$cid])) {
            $channelarray = $GLOBALS['nav'][$cid];
        } elseif (function_exists($GLOBALS['nav'][$cid])) {
            $channelarray = $GLOBALS['nav'][$cid]($cid);
        }

    }
    foreach ($channelarray as $key => $channel) {
        $thisstr = $where['html'];
        if (!isset($channel['onstyle']) || empty($channel['onstyle'])) {
            if (in_array($channel['cid'], $onarray)) {
                $channel['onstyle'] = 'active';
            } else {
                $channel['onstyle'] = '';
            }
        }
        if (defined('cid') && cid == 0 && $channel['cid'] == 0) {//homepage
            $channel['onstyle'] = 'active';
        }
        $returnarray[] = $channel;
        if (!isset($where['returnarray']) || $where['returnarray'] == 0) {
            $thisstr = str_replace('{url}', @$channel['url'], $thisstr);
            $thisstr = str_replace('{target}', @$channel['target'], $thisstr);
            $thisstr = str_replace('{name}', @$channel['name'], $thisstr);
            $thisstr = str_replace('{onstyle}', @$channel['onstyle'], $thisstr);
            $returnstr .= $thisstr . "\r\n";
        }
    }
    if (!isset($where['returnarray']) || $where['returnarray'] == 0) {
        return $returnstr;
    } else {
        return $returnarray;
    }
}

/**
 * 获取面包屑
 * @param string $cid
 * @param bool $returnarray
 * @param string $linktag
 * @return array|string
 */
function uuu_breadcrubm($cid = '', $returnarray = false, $linktag = ' &gt; ')
{
    if (empty($cid)) {
        if (defined('cid')) {
            $cid = cid;
        } else {
            $cid = 0;
        }
    }
    $channels = uuu_getchannelscache();
    $breadcrumb = '';
    $breadcrumbarray = array();
    while (true) {
        $thischannel = false;
        foreach ($channels as $channel) {
            if ($channel['cid'] == $cid) {
                $thischannel = $channel;
            }
        }
        if ($thischannel == false) {
            if ($breadcrumb == '') {
                $breadcrumb = '<a href="' . qqq_homeurl() . '">首页</a>';
            } else {
                $breadcrumb = '<a href="' . qqq_homeurl() . '">首页</a>' . $linktag . $breadcrumb;
            }
            if ($returnarray) {
                return array_reverse($breadcrumbarray);
            } else {
                return $breadcrumb;
            }
        } else {
            if ($breadcrumb == '') {
                $breadcrumb = '<a href="' . $thischannel['url'] . '">' . $thischannel['name'] . '</a>';
            } else {
                $breadcrumb = '<a href="' . $thischannel['url'] . '">' . $thischannel['name'] . '</a>' . $linktag . $breadcrumb;
            }
            $breadcrumbarray[] = $thischannel;
            $cid = $thischannel['fid'];
        }
    }
}

/**
 * 从缓存获取所有栏目
 * @return array|mixed
 */
function uuu_getchannelscache()
{
    if (isset($GLOBALS['channelscache'])) {
        return $GLOBALS['channelscache'];
    }
    $channelcachekeyname = 'channelscache';
    $channelcache = cache_site_get($channelcachekeyname, 604800, 'channel');
    if ($channelcache) {
        $channels = json_decode($channelcache, 1);
    } else {
        $channelslist = M()->all("SELECT cid,fid,ifshownav,cname,ckind,cvalue,newwindow,csetting FROM " . tableex('channel') . " where ckind<>'4' and ifshow=1 order by corder asc");
        if ($channelslist) {
            $channels = array();
            foreach ($channelslist as $val) {
                unset($thischannel);
                $thischannel = array();
                $thischannel['cid'] = $val['cid'];
                $thischannel['fid'] = $val['fid'];
                $thischannel['ckind'] = $val['ckind'];
                $thischannel['ifshownav'] = $val['ifshownav'];
                $thischannel['name'] = $val['cname'];
                if ($val['newwindow'] == 1) {
                    $thischannel['target'] = ' target="_blank" ';
                } else {
                    $thischannel['target'] = '';
                }
                $thischannel['url'] = uuu_getchannelurl($val);
                if (empty($thischannel['url'])) {
                    $thischannel['url'] = '#';
                    $thischannel['target'] = '';
                }
                $channels[] = $thischannel;
            }
            cache_site_set($channelcachekeyname, json_encode($channels), 604800, 'channel');
        } else {
            return array();
        }
    }
    $GLOBALS['channelscache'] = $channels;
    return $channels;
}

/**
 * 从缓存获取栏目
 * @param $cid
 * @return array|bool|false|mixed|string
 */
function uuu_getchannelcache($cid)
{
    if (isset($GLOBALS['channelcache'][$cid])) {
        return $GLOBALS['channelcache'][$cid];
    }
    $channelcachekeyname = 'channelcache_' . $cid;
    $channelcache = cache_site_get($channelcachekeyname, 604800, 'channel');
    if ($channelcache) {
        $channelcache = json_decode($channelcache, 1);
    } else {
        if (is_numeric($cid)) {
            $channelcache = M()->one("SELECT * FROM " . tableex('channel') . " where cid='$cid' limit 1");
        } else {
            $channelcache = M()->one("SELECT * FROM " . tableex('channel') . " where cname='$cid' limit 1");
        }
        if ($channelcache) {
            $channelcache['csetting'] = json_decode($channelcache['csetting'], 1);
            cache_site_set($channelcachekeyname, json_encode($channelcache), 604800, 'channel');
        } else {
            return false;
        }
    }
    $GLOBALS['channelcache'][$cid] = $channelcache;
    return $channelcache;
}

/**
 * 获取栏目名
 * @param string $cid cid
 * @return mixed
 */
function uuu_getchannelname($cid)
{
    $c = uuu_getchannelcache($cid);
    return $c['cname'];
}

/**
 * 同级别栏目
 * @param string $cid cid
 * @return array
 */
function uuu_getchannels_samelevel($cid)
{
    $c = uuu_getchannelcache($cid);
    if ($c['fid'] == 0) {
        $channels = array();
    } else {
        $channels = uuu_channel_list(0, 999, $c['fid']);
    }

    return $channels;
}

/**
 * 获取父栏目名
 * @param string $cid cid
 * @return array|bool|false|mixed|string
 */
function uuu_getchannelname_father($cid)
{
    $c = uuu_getchannelcache($cid);
    if ($c['fid'] == 0) {
        $c = uuu_getchannelname($cid);
    } else {
        $c = uuu_getchannelname($c['fid']);
    }
    return $c;
}

/**
 * 获取栏目链接
 * @param $cid
 * @param null $key_array
 * @return bool|mixed|string
 */
function uuu_getchannelurl($cid, $key_array = null)
{
    if (!is_array($cid)) {
        $cid = uuu_getchannelcache($cid);
        if (!$cid) {
            return false;
        }
        $csetting = $cid['csetting'];
    } else {
        $csetting = json_decode($cid['csetting'], 1);
    }
    $channel = $cid;
    if ($channel['ckind'] == 3 || $channel['ckind'] == 4 || $channel['ckind'] == 5) {
        return $channel['cvalue'];
    } elseif ($channel['ckind'] == 1 || $channel['ckind'] == 2) {
        if (isset($csetting['template']) && empty($csetting['template'])) {
            return '';
        }
    }
    if (!isset($csetting['channel_url'])) {
        return '';
    }
    $channelurls = explode(';', $csetting['channel_url']);
    $channelurl = str_replace("(cid)", $cid['cid'], $channelurls[0]);
    if ($key_array) {
        foreach ($key_array as $key => $val) {
            $channelurl = str_replace("(" . $key . ")", $val, $channelurl);
            $channelurl = str_replace("{" . $key . "}", $val, $channelurl);
        }
    }


    if (empty($channelurl)) {
        return '';
    }

    if (!isset($csetting['channel_domain']) || empty($csetting['channel_domain'])) {
        $csetting['channel_domain'] = SystemDomain;
    }
    if (isset($csetting['channel_domain']) && $csetting['channel_domain'] <> '') {
        $channel_domains = explode(';', $csetting['channel_domain']);
        $csetting['channel_domain'] = $channel_domains[0];
        foreach ($channel_domains as $channel_domain) {
            if (stripos($channel_domain, '*') === false) {
                $csetting['channel_domain'] = $channel_domain;
                break;
            }
        }
        foreach ($channel_domains as $channel_domain) {
            if (server_name() == $channel_domain) {
                $csetting['channel_domain'] = '';
                break;
            }
        }
    }
    if (UrlRewrite) {
        if (empty($csetting['channel_domain'])) {
            $thisurl = SystemDir . $channelurl;
            $thisurl = str_replace("//", "/", $thisurl);
        } else {
            $thisurl = '//' . $csetting['channel_domain'] . server_port() . $channelurl;
        }
    } else {
        if (empty($csetting['channel_domain'])) {
            $thisurl = SystemDir . IndexFile . $channelurl;
        } else {
            $thisurl = '//' . $csetting['channel_domain'] . server_port() . SystemDir . IndexFile . $channelurl;
        }
    }
    return $thisurl;
}


/**
 * 获取栏目的变量
 * @param $strname
 * @param string $cid
 * @param int $return
 * @return bool|mixed
 */
function uuu_str($strname, $cid = '', $return = 1)
{
    if ($strname == '') {
        return false;
    }
    if (defined('cid') && $cid === '') {
        $cid = cid;
    } elseif ($cid === 0) {
        $cid = 0;
    } else {
        if (is_numeric($cid)) {
            $cid = intval($cid);
        } else {
            $cinfo = uuu_getchannelcache($cid);
            if ($cinfo) {
                $cid = $cinfo['cid'];
            } else {
                return false;
            }
        }
    }
    $strname = dbstr($strname);
    if (isset($GLOBALS['strget'][$cid][$strname])) {
        if ($return == 1) {
            return $GLOBALS['strget'][$cid][$strname];
        } else {
            echo($GLOBALS['strget'][$cid][$strname]);
            return true;
        }
    }
    $cachehash = $strname . '_' . $cid;
//      dump_pre("获取" . $cachehash);
    $res = cache_site_get($cachehash, 3600 * 24 * 365, 'str');
    if ($res !== false) {
        $GLOBALS['strget'][$cid][$strname] = $res;
        if ($return == 1) {
            return $GLOBALS['strget'][$cid][$strname];
        } else {
            echo($GLOBALS['strget'][$cid][$strname]);
            return true;
        }
    }

    $sql = "SELECT strcid,strvalue FROM " . tableex('str') . " where strname='$strname' and strcid='$cid' limit 1;";
//    dump_pre($sql);
    $link = M()->one($sql);

    if ($link) {
        $GLOBALS['strget'][$cid][$strname] = $link['strvalue'];
        $cachehash = $strname . '_' . $cid;
        cache_site_set($cachehash, $link['strvalue'], 3600 * 24 * 365, 'str');
        if ($return == 1) {
            return $GLOBALS['strget'][$cid][$strname];
        } else {
            echo($GLOBALS['strget'][$cid][$strname]);
            return true;
        }
    }
    $GLOBALS['strget'][$cid][$strname] = false;
    return false;
}

/**
 * 设置栏目的变量
 * @param $strname
 * @param $strvalue
 * @param int $cid
 * @return bool
 */
function uuu_strset($strname, $strvalue, $cid = 0)
{
    if ($strname == '') {
        return false;
    }
    if (is_numeric($cid)) {
        $cid = intval($cid);
    } else {
        $cinfo = uuu_getchannelcache($cid);
        if ($cinfo) {
            $cid = $cinfo['cid'];
        } else {
            return false;
        }
    }
    $strname = dbstr($strname);
    $strvalue = dbstr($strvalue);
    $query = M()->query("UPDATE " . tableex('str') . " SET strvalue='$strvalue' WHERE strname='$strname' and strcid='$cid';");
    if ($query) {
        $GLOBALS['strget'][$cid][$strname] = $strvalue;
        $cachehash = $strname . '_' . $cid;
        cache_site_set($cachehash, $strvalue, 3600 * 24 * 365, 'str');
        return true;
    } else {
        return false;
    }
}
